package com.weather.modules.service.impl;

import com.weather.common.config.RabbitConfig;
import com.weather.common.utils.JsonUtils;
import com.weather.common.vo.ForecastVo;
import com.weather.common.vo.WeatherVo;
import com.weather.common.vo.YesterdayVo;
import com.weather.modules.entity.Forecast;
import com.weather.modules.entity.Weather;
import com.weather.modules.entity.Yesterday;
import com.weather.modules.service.ForecastService;
import com.weather.modules.service.MqWeatherDataServer;
import com.weather.modules.service.WeatherService;
import com.weather.modules.service.YesterdayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MqWeatherDataServerImpl implements MqWeatherDataServer {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private YesterdayService yesterdayService;

    @Autowired
    private ForecastService forecastService;

    @Resource
    private AmqpTemplate rabbitTemplate;

    @Override
    @RabbitListener(queues = RabbitConfig.TOPICWEATHER)
    @Transactional(rollbackFor = Exception.class)
    public void putMqWeatherData(String message) {
        System.err.println(message);
        /* JSONObject json_tata = JSONObject.parseObject(message);*/
        WeatherVo weather = JsonUtils.jsonToPojo(message +"", WeatherVo.class);
        if (weather != null) {
            try {
                Weather bean = new Weather();
                bean.setCity(weather.getCity());
                bean.setCityId(weather.getCityId());
                bean.setGanmao(weather.getGanmao());
                bean.setWendu(weather.getWendu());
                this.weatherService.saveOrUpdate(bean);

                // 写入更新历史天气数据
                YesterdayVo yesterdayVo = weather.getYesterday();
                Yesterday yesterday = new Yesterday();
                yesterday.setCityId(weather.getCityId());
                yesterday.setDate(yesterdayVo.getDate());
                yesterday.setFl(yesterdayVo.getFl());
                yesterday.setFx(yesterdayVo.getFx());
                yesterday.setHigh(yesterdayVo.getHigh());
                yesterday.setLow(yesterdayVo.getLow());
                yesterday.setType(yesterdayVo.getType());
                this.yesterdayService.saveOrUpdate(yesterday);

                // 写入未来几天的天气数据
                List<ForecastVo> forecast = weather.getForecast();
                List<Forecast> listbean = new ArrayList<Forecast>();
                Forecast forecastbean = new Forecast();
                forecastbean.setCityId(weather.getCityId());
                for (int i = 0 ,j = forecast.size(); i < j; i++) {
                    forecastbean.setDate(forecast.get(i).getDate());
                    forecastbean.setFengli(forecast.get(i).getFengli());
                    forecastbean.setFengxiang(forecast.get(i).getFengxiang());
                    forecastbean.setHigh(forecast.get(i).getHigh());
                    forecastbean.setLow(forecast.get(i).getLow());
                    forecastbean.setType(forecast.get(i).getType());
                    listbean.add(forecastbean);
                }
                this.forecastService.saveOrUpdateBatch(listbean,listbean.size());
                this.logger.info("install data successfully !!! - [{}]",System.currentTimeMillis());
            } catch (Exception e) {
                this.logger.info("install data successfully !!! - [{}]",System.currentTimeMillis());
                // TODO 这个入库失败要重新推送到消息队列之中 重新做业务逻辑处理
                this.rabbitTemplate.convertAndSend(RabbitConfig.TOPICEXCHANGE,RabbitConfig.ROUTING_WEATHERDIEKEY,message);
            }
        }else{
            this.logger.info("message data filed !!! - [{}]",System.currentTimeMillis());
        }

    }

}
